package n17.second;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class F04 {
	public static void Hi(String who) {
		System.out.println("Hello：");
		System.out.println("   "+who);
	}
	public static String run(Code code) {
		try(
			ByteArrayOutputStream bs = new ByteArrayOutputStream();
			PrintStream ps=new PrintStream(bs, true, "utf-8")){
			PrintStream out=System.out; //保存原来的标准输出
			System.setOut(ps); //设置新的标准输出
			code.getResult(); //运行代码
			System.setOut(out);
			return bs.toString("utf-8"); //返回代码运行时显示到屏幕的内容
		}catch(Exception e) {
			return null;
		}
		
	}
	
	public static void main(String[] args) {
		String r=run(()->{
			System.out.println("How are you?");
			System.out.println("I'm fine, thank you.");
		});
		System.out.println(r);
		
		r=run(()->{
			Hi("张三");
		});
		System.out.println(r);
	}


}
